import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
    },
    {
      path: '/visitor',
      component: () => import('@/views/MapLayout.vue'),
      meta: { requiresAuth: true, role: 'visitor' },
      children: [
        {
          path: '',
          redirect: '/visitor/map-browsing',
        },
        {
          path: 'map-browsing',
          name: 'map-browsing',
          component: () => import('@/components/visitor/MapBrowsingView.vue'),
          meta: { requiresAuth: true, role: 'visitor' },
        },
        {
          path: 'geographical-query',
          name: 'geographical-query',
          component: () => import('@/components/visitor/GeographicalQueryView.vue'),
          meta: { requiresAuth: true, role: 'visitor' },
        },
        {
          path: 'facility-search',
          name: 'facility-search',
          component: () => import('@/components/visitor/FacilitySearchView.vue'),
          meta: { requiresAuth: true, role: 'visitor' },
        },
        {
          path: 'traffic-info',
          name: 'traffic-info',
          component: () => import('@/components/visitor/TrafficInfoView.vue'),
          meta: { requiresAuth: true, role: 'visitor' },
        },
        {
          path: 'scenic-recommendation',
          name: 'scenic-recommendation',
          component: () => import('@/components/visitor/ScenicRecommendationView.vue'),
          meta: { requiresAuth: true, role: 'visitor' },
        },
      ],
    },
    {
      path: '/map',
      redirect: '/visitor/map-browsing',
      meta: { requiresAuth: true, role: 'visitor' },
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('@/views/MapLayout.vue'),
      meta: { requiresAuth: true, role: 'admin' },
      children: [
        {
          path: '',
          redirect: '/admin/analysis',
        },
        {
          path: 'analysis',
          name: 'admin-analysis',
          component: () => import('@/components/AnalysisView.vue'),
          meta: { requiresAuth: true, role: 'admin' },
        },
        {
          path: 'management',
          name: 'admin-management',
          component: () => import('@/components/ManagementView.vue'),
          meta: { requiresAuth: true, role: 'admin' },
        },
      ],
    },
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const requiresAuth = to.matched.some((record) => record.meta.requiresAuth)
  const requiredRole = to.matched.find((record) => record.meta.role)?.meta.role

  if (requiresAuth && !userStore.isLogin) {
    // 未登录，重定向到登录页
    next('/login')
  } else if (requiredRole && userStore.userRole !== requiredRole) {
    // 角色不匹配，根据当前角色重定向
    if (userStore.userRole === 'admin') {
      next('/admin')
    } else if (userStore.userRole === 'visitor') {
      next('/visitor/map-browsing')
    } else {
      next('/login')
    }
  } else {
    // 正常导航
    next()
  }
})

export default router
